home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / programr / ole2book.zip / CHAP10.ZIP / CHAP10 / SCHMOO / FIGURE.CPP < prev    next >
C/C++ Source or Header  |  1993-06-13  |  7KB  |  320 lines

  1. /*
  2.  * FIGURE.CPP
  3.  * Schmoo Figure Object Chapter 10
  4.  *
  5.  * Implementation of the CFigure object for Schmoo.
  6.  *
  7.  * Copyright (c)1993 Microsoft Corporation, All Rights Reserved
  8.  *
  9.  * Kraig Brockschmidt, Software Design Engineer
  10.  * Microsoft Systems Developer Relations
  11.  *
  12.  * Internet  :  kraigb@microsoft.com
  13.  * Compuserve:  >INTERNET:kraigb@microsoft.com
  14.  */
  15.  
  16.  
  17. #include "schmoo.h"
  18.  
  19.  
  20. /*
  21.  * CFigure::CFigure
  22.  * CFigure::~CFigure
  23.  *
  24.  * Parameters (Constructor):
  25.  *  pfnDestroy      LPFNDESTROYED to call when an object is destroyed.
  26.  *  pDoc            LPCSchmooDoc we're associated with.
  27.  */
  28.  
  29. CFigure::CFigure(LPFNDESTROYED pfnDestroy, LPCSchmooDoc pDoc)
  30.     {
  31.     m_cRef=0;
  32.     m_pfnDestroy=pfnDestroy;
  33.  
  34.     m_pFR=NULL;     //We get this later through FrameSet.
  35.     m_pDoc=pDoc;
  36.     m_pPL=pDoc->m_pPL;
  37.  
  38.     m_fEmbedded=FALSE;
  39.  
  40.     //NULL any contained interfaces initially.
  41.     m_pIPersistStorage=NULL;
  42.     m_pIDataObject=NULL;
  43.     m_pIDataAdviseHolder=NULL;
  44.     m_pIOleObject=NULL;
  45.     m_pIOleAdviseHolder=NULL;
  46.     m_pIOleClientSite=NULL;
  47.  
  48.     m_cf=pDoc->m_cf;
  49.  
  50.     //These are for IDataObject::QueryGetData
  51.     m_cfeGet=CFORMATETCGET;
  52.  
  53.     SETDefFormatEtc(m_rgfeGet[0], pDoc->m_cf, TYMED_HGLOBAL);
  54.     SETDefFormatEtc(m_rgfeGet[1], pDoc->m_cfEmbedSource, TYMED_ISTORAGE);
  55.     SETDefFormatEtc(m_rgfeGet[2], pDoc->m_cfObjectDescriptor, TYMED_HGLOBAL);
  56.     SETDefFormatEtc(m_rgfeGet[3], CF_METAFILEPICT, TYMED_MFPICT);
  57.     SETDefFormatEtc(m_rgfeGet[4], CF_BITMAP, TYMED_GDI);
  58.  
  59.     return;
  60.     }
  61.  
  62.  
  63. CFigure::~CFigure(void)
  64.     {
  65.     //Free contained interfaces.
  66.     if (NULL!=m_pIOleObject)
  67.         delete m_pIOleObject;
  68.  
  69.     if (NULL!=m_pIDataObject)
  70.         delete m_pIDataObject;
  71.  
  72.     if (NULL!=m_pIPersistStorage)
  73.         delete m_pIPersistStorage;
  74.  
  75.     return;
  76.     }
  77.  
  78.  
  79.  
  80.  
  81.  
  82. /*
  83.  * CFigure::QueryInterface
  84.  * CFigure::AddRef
  85.  * CFigure::Release
  86.  *
  87.  * Purpose:
  88.  *  IUnknown members for CFigure object.
  89.  */
  90.  
  91. STDMETHODIMP CFigure::QueryInterface(REFIID riid, LPVOID FAR *ppv)
  92.     {
  93.     *ppv=NULL;
  94.  
  95.     if (IsEqualIID(riid, IID_IUnknown))
  96.         *ppv=(LPVOID)this;
  97.  
  98.     if (IsEqualIID(riid, IID_IPersist) || IsEqualIID(riid, IID_IPersistStorage))
  99.         *ppv=(LPVOID)m_pIPersistStorage;
  100.  
  101.     if (IsEqualIID(riid, IID_IDataObject))
  102.         *ppv=(LPVOID)m_pIDataObject;
  103.  
  104.     if (IsEqualIID(riid, IID_IOleObject))
  105.         *ppv=(LPVOID)m_pIOleObject;
  106.  
  107.     //AddRef any interface we'll return.
  108.     if (NULL!=*ppv)
  109.         {
  110.         ((LPUNKNOWN)*ppv)->AddRef();
  111.         return NOERROR;
  112.         }
  113.  
  114.     return ResultFromScode(E_NOINTERFACE);
  115.     }
  116.  
  117.  
  118. STDMETHODIMP_(ULONG) CFigure::AddRef(void)
  119.     {
  120.     return ++m_cRef;
  121.     }
  122.  
  123.  
  124. STDMETHODIMP_(ULONG) CFigure::Release(void)
  125.     {
  126.     ULONG       cRefT;
  127.  
  128.     cRefT=--m_cRef;
  129.  
  130.     if (0==m_cRef)
  131.         {
  132.         /*
  133.          * Tell the housing that an object is going away so it can
  134.          * shut down if appropriate.
  135.          */
  136.         if (NULL!=m_pfnDestroy)
  137.             (*m_pfnDestroy)();
  138.  
  139.         delete this;
  140.         }
  141.  
  142.     return cRefT;
  143.     }
  144.  
  145.  
  146.  
  147.  
  148.  
  149.  
  150. /*
  151.  * CFigure::FInit
  152.  *
  153.  * Purpose:
  154.  *  Performs any intiailization of a CFigure that's prone to failure
  155.  *  that we also use internally before exposing the object outside.
  156.  *
  157.  * Parameters:
  158.  *  None
  159.  *
  160.  * Return Value:
  161.  *  BOOL            TRUE if the function is successful, FALSE otherwise.
  162.  */
  163.  
  164. BOOL CFigure::FInit(void)
  165.     {
  166.     //Allocate contained interfaces.
  167.     m_pIPersistStorage=new CImpIPersistStorage(this, (LPUNKNOWN)this);
  168.  
  169.     if (NULL==m_pIPersistStorage)
  170.         return FALSE;
  171.  
  172.     m_pIDataObject=new CImpIDataObject(this, (LPUNKNOWN)this);
  173.  
  174.     if (NULL==m_pIDataObject)
  175.         return FALSE;
  176.  
  177.     m_pIOleObject=new CImpIOleObject(this, (LPUNKNOWN)this);
  178.  
  179.     if (NULL==m_pIOleObject)
  180.         return FALSE;
  181.  
  182.     return TRUE;
  183.     }
  184.  
  185.  
  186.  
  187. /*
  188.  * CFigure::FrameSet
  189.  *
  190.  * Purpose:
  191.  *  Provides the compound document object with access to the frame
  192.  *  of this application for UI purposes.
  193.  *
  194.  * Parameters:
  195.  *  pFR             LPCSchmooFrame of the frame window.
  196.  *
  197.  * Return Value:
  198.  *  None
  199.  */
  200.  
  201. void CFigure::FrameSet(LPCSchmooFrame pFR)
  202.     {
  203.     m_pFR=pFR;
  204.     return;
  205.     }
  206.  
  207.  
  208.  
  209.  
  210. /*
  211.  * CFigure::FIsDirty
  212.  *
  213.  * Purpose:
  214.  *  Checks if the document is dirty.  This can be called from
  215.  *  IPersistStorage::IsDirty which doesn't have access to CSchmooDoc.
  216.  *
  217.  * Parameters:
  218.  *  None
  219.  *
  220.  * Return Value:
  221.  *  BOOL            TRUE if dirty, FALSE if clean.
  222.  */
  223.  
  224. BOOL CFigure::FIsDirty(void)
  225.     {
  226.     return m_pDoc->m_fDirty;
  227.     }
  228.  
  229.  
  230.  
  231.  
  232. /*
  233.  * CFigure::FIsEmbedded
  234.  *
  235.  * Purpose:
  236.  *  Answers if the object is embedded or not.
  237.  *
  238.  * Parameters:
  239.  *  None
  240.  *
  241.  * Return Value:
  242.  *  BOOL            TRUE if the object is embedded, FALSE otherwise.
  243.  */
  244.  
  245. BOOL CFigure::FIsEmbedded(void)
  246.     {
  247.     return m_fEmbedded;
  248.     }
  249.  
  250.  
  251.  
  252.  
  253. /*
  254.  * CFigure::SendAdvise
  255.  *
  256.  * Purpose:
  257.  *  Calls the appropriate IOleClientSite or IAdviseSink member function
  258.  *  for various events such as closure, renaming, saving, etc.
  259.  *
  260.  * Parameters:
  261.  *  uCode           UINT OBJECTCODE_* identifying the notification.
  262.  *
  263.  * Return Value:
  264.  *  None
  265.  */
  266.  
  267. void CFigure::SendAdvise(UINT uCode)
  268.     {
  269.     switch (uCode)
  270.         {
  271.         case OBJECTCODE_SAVED:
  272.             if (NULL!=m_pIOleAdviseHolder)
  273.                 m_pIOleAdviseHolder->SendOnSave();
  274.             break;
  275.  
  276.         case OBJECTCODE_CLOSED:
  277.             if (NULL!=m_pIOleAdviseHolder)
  278.                 m_pIOleAdviseHolder->SendOnClose();
  279.  
  280.             break;
  281.  
  282.         case OBJECTCODE_RENAMED:
  283.             //Call IOleAdviseHolder::SendOnRename (later)
  284.             break;
  285.  
  286.         case OBJECTCODE_SAVEOBJECT:
  287.             if (m_pDoc->m_fDirty && NULL!=m_pIOleClientSite)
  288.                 m_pIOleClientSite->SaveObject();
  289.  
  290.             break;
  291.  
  292.         case OBJECTCODE_DATACHANGED:
  293.             //No flags are necessary here.
  294.             if (NULL!=m_pIDataAdviseHolder)
  295.                 m_pIDataAdviseHolder->SendOnDataChange(m_pIDataObject, 0, 0);
  296.  
  297.             break;
  298.  
  299.         case OBJECTCODE_SHOWWINDOW:
  300.             if (NULL!=m_pIOleClientSite)
  301.                 m_pIOleClientSite->OnShowWindow(TRUE);
  302.  
  303.             break;
  304.  
  305.         case OBJECTCODE_HIDEWINDOW:
  306.             if (NULL!=m_pIOleClientSite)
  307.                 m_pIOleClientSite->OnShowWindow(FALSE);
  308.  
  309.             break;
  310.  
  311.         case OBJECTCODE_SHOWOBJECT:
  312.             if (NULL!=m_pIOleClientSite)
  313.                 m_pIOleClientSite->ShowObject();
  314.  
  315.             break;
  316.         }
  317.  
  318.     return;
  319.     }
  320.